package com.wwtech.epp.ui.adapter.main;

import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseMultiItemQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.chad.library.adapter.base.entity.MultiItemEntity;
import com.socks.library.KLog;
import com.wwtech.epp.R;
import com.wwtech.epp.contain.ContainValue;
import com.wwtech.epp.db.Friend;
import com.wwtech.epp.entity.multi.ParentItem;
import com.wwtech.epp.entity.multi.PersonItem;
import com.wwtech.epp.view.GlideCircleTransform;

import java.util.List;

/**
 * Created by hu on 2017/9/11.
 */

public class CheckPersonAdapter extends BaseMultiItemQuickAdapter<MultiItemEntity, BaseViewHolder> {

    public static final int TYPE_PARENT=0;
    public static final int TYPE_PERSON=1;

    public boolean isSingleChoose = true;
    private OnPersonCheckChange onCheckChange;

    private OnItemClickListenerme onItemClickListener;

    private boolean canDelet = true;

    public boolean isCanDelet() {
        return canDelet;
    }

    public void setCanDelet(boolean canDelet) {
        this.canDelet = canDelet;
    }

    public CheckPersonAdapter(List<MultiItemEntity> data) {
        super(data);
        addItemType(TYPE_PARENT, R.layout.item_contact_parent);
        addItemType(TYPE_PERSON, R.layout.item_contact_person);
    }


    @Override
    protected void convert(BaseViewHolder baseViewHolder, MultiItemEntity item) {
        switch (baseViewHolder.getItemViewType()) {
            case TYPE_PARENT:
                final ParentItem lv0 = (ParentItem)item;
                baseViewHolder.setText(R.id.name, lv0.title);
                ImageView zhishi = baseViewHolder.getView(R.id.iv_zhishi);
                ImageView avater = baseViewHolder.getView(R.id.avater);
                if (lv0.parentType.equals("zuzhi")) {
                    Glide.with(mContext)
                            .load(R.mipmap.ic_launcher)
                            .transform(new GlideCircleTransform(mContext))
                            .into(avater);
                } else {
                    Glide.with(mContext)
                            .load(R.mipmap.mail_group)
                            .transform(new GlideCircleTransform(mContext))
                            .into(avater);
                }
                if (lv0.isExpanded()) {
                    zhishi.setImageDrawable(mContext.getResources().getDrawable(R.mipmap.drop_down));
                } else {
                    zhishi.setImageDrawable(mContext.getResources().getDrawable(R.mipmap.drop_up));
                }
                baseViewHolder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        int pos = baseViewHolder.getAdapterPosition();
                        KLog.i(TAG, "Level 0 item pos: " + pos);
                        KLog.i(lv0.isExpanded());
                        if (lv0.isExpanded()) {
                            collapse(pos);
                            KLog.i(ContainValue.userList.size());
                        } else {
                            expand(pos);
                            KLog.i(ContainValue.userList.size());
                            if (lv0.parentType.equals("contact")) {
                                if (onCheckChange != null) {
                                    onCheckChange.onContactItemOpen();
                                }
                            }
                        }
                    }
                });
                break;
            case TYPE_PERSON:
                final PersonItem lv1 = (PersonItem)item;
                ImageView imageView = baseViewHolder.getView(R.id.avater);
                if (lv1.type.equals("zuzhi")) {
                    baseViewHolder.setText(R.id.name, lv1.node.getName());
                    baseViewHolder.setVisible(R.id.check_box, false);
                    Glide.with(mContext)
                            .load(R.mipmap.zuzhijiagou)
                            .transform(new GlideCircleTransform(mContext))
                            .into(imageView);
                    baseViewHolder.itemView.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            onItemClickListener.onItemClick(lv1);
                        }
                    });
                }else if (lv1.type.equals("contact")) {
                    baseViewHolder.setText(R.id.name, lv1.freind.getNickname());
                    CheckBox checkBox = baseViewHolder.getView(R.id.check_box);
                    if (!isSingleChoose) {
                        baseViewHolder.setVisible(R.id.check_box, true);
                    }
                    if (!canDelet) {
                        if (lv1.freind.isCanCheck()) {
                            checkBox.setEnabled(true);
                        } else {
                            KLog.i("适配器中，设置不能被选中");
                            checkBox.setEnabled(false);
                        }
                    } else {
                        checkBox.setEnabled(true);
                    }
                    Glide.with(mContext)
                            .load(lv1.freind.getAvatar())
                            .transform(new GlideCircleTransform(mContext))
                            .into(imageView);
                    baseViewHolder.itemView.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            onItemClickListener.onItemClick(lv1);
                        }
                    });
//                    checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
//                        @Override
//                        public void onCheckedChanged(CompoundButton buttonView, boolean getChecked) {
//                            onCheckChange.onPersonCheckChange(lv1.freind, getChecked);
//                        }
//                    });
                    baseViewHolder.setOnCheckedChangeListener(R.id.check_box, new CompoundButton.OnCheckedChangeListener() {
                        @Override
                        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                            if (onCheckChange != null) {
                                onCheckChange.onPersonCheckChange(lv1.freind, isChecked);
                            }
                        }
                    });
                    if (lv1.freind.getChecked()) {
                        KLog.i(lv1.freind.getNickname());
                        checkBox.setChecked(true);
                    } else {
                        checkBox.setChecked(false);
                    }
//        if (!friend.isCanCheck()) {
//            KLog.i("设置不能被选中");
//            //不能被选中
//            checkBox.setEnabled(false);
//            checkBox.setBackground(mContext.getResources().getDrawable(R.mipmap.multiselect_selected));
//        } else {
//            checkBox.setEnabled(true);
//            checkBox.setBackground(mContext.getResources().getDrawable(R.drawable.check_person_selector));
                } else if (lv1.type.equals("qunzu")) {
                    baseViewHolder.setVisible(R.id.check_box, false);
                    baseViewHolder.setText(R.id.name, lv1.group.getNickName());
                    Glide.with(mContext)
                            .load(lv1.group.getAvatar())
                            .transform(new GlideCircleTransform(mContext))
                            .into(imageView);
                    baseViewHolder.itemView.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            onItemClickListener.onItemClick(lv1);
                        }
                    });
                }
                break;
            default:
                break;
        }
    }

    public interface OnItemClickListenerme {
        void onItemClick(PersonItem item);
    }

    public void setOnItemClick(OnItemClickListenerme onItemClick) {
        onItemClickListener = onItemClick;
    }
    public interface OnPersonCheckChange {
        void onPersonCheckChange(Friend friend, boolean isChecked);
        void onContactItemOpen();
    }

    public void setOncheckChange(OnPersonCheckChange oncheckChange) {
        this.onCheckChange = oncheckChange;
    }
}
